package com.dspsemi.wodi;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {

	private EditText name;
	private EditText password;
	private EditText password2;
	private Button register;
	private Handler mHandler;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		initViews();
		setListeners();
		initHandler();

	}

	private void initHandler() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				pd.dismiss();

				if (msg.what == 1) {
					// 注册成功

					String nameStr = name.getText().toString();
					String passwordStr = password.getText().toString();

					SharedPreferences sp = getSharedPreferences("wodi", 0);
					sp.edit().putString("name", nameStr).commit();
					sp.edit().putString("password", passwordStr).commit();
					sp.edit().putString("nickname", "");
					sp.edit().putString("jifen", "0").commit();
					sp.edit().putString("sheng", "0").commit();
					sp.edit().putString("bai", "0").commit();

					Intent i = new Intent();
					i.setClass(RegisterActivity.this, InitInfoActivity.class);
					startActivity(i);
					finish();
					return;
				}
				if (msg.what == 0) {
					toast("注册失败");
					return;
				}
			}
		};
	}

	private void initViews() {
		name = (EditText) findViewById(R.id.name);
		password = (EditText) findViewById(R.id.password);
		password2 = (EditText) findViewById(R.id.password2);
		register = (Button) findViewById(R.id.register);

		pd = new ProgressDialog(this);
		pd.setMessage("注册中");
		pd.setCancelable(false);
	}

	private void setListeners() {
		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String nameStr = name.getText().toString();
				String passwordStr = password.getText().toString();
				String passwordStr2 = password2.getText().toString();

				if (nameStr == null || nameStr.length() == 0) {
					toast("请输入帐号");
					return;
				}
				if (passwordStr == null || passwordStr.length() == 0) {
					toast("请输入密码");
					return;
				}
				if (passwordStr2 == null || passwordStr.length() == 0) {
					toast("请输入密码确认");
					return;
				}
				if (!passwordStr2.equals(passwordStr)) {
					toast("两次输入的密码不一致");
					return;
				}

				register(nameStr, passwordStr);
			}
		});

	}

	protected void register(String nameStr, String passwordStr) {
		pd.show();
		new Thread() {
			public void run() {
				mHandler.sendEmptyMessage(1);
			};
		}.start();
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
}
